﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Fish_Atlantis.SupplierPanel
{
    public partial class Supplier : System.Web.UI.MasterPage
    {
        static public string username = "用户";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] == null && !check_cookie())//登录时保存在Session的值 
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('您还未登录，请登录后再操作！！');location.href='/Account/Login.aspx';</script>");
            }
            else
            {
                username = Session["UserName"].ToString();
                string nowUserType = Helper.ExecuteScalar("select User_Type from Users where UserID = '" + Session["UserID"] + "'");//从Users表获取用户ID对应的用户Type
                if (nowUserType != "2")
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('操作失败！无权限访问Supplier页面！请切换Supplier账号登陆！');location.href='/Account/Login.aspx';</script>");
                }
            }
        }
        protected void GetTypeName(object sender, EventArgs e)
        {
            if (Response.Cookies["UserNameCookie"] != null)
            {
                HttpCookie myCookie = new HttpCookie("UserNameCookie");
                myCookie.Expires = DateTime.Now.AddDays(-1d);
                Response.Cookies.Add(myCookie);
            }
            if (Response.Cookies["UserPasswordCookie"] != null)
            {
                HttpCookie myCookie = new HttpCookie("UserPasswordCookie");
                myCookie.Expires = DateTime.Now.AddDays(-1d);
                Response.Cookies.Add(myCookie);
            }
            Session["UserID"] = null;
            Session["UserName"] = null;
            Session["User_Type"] = null;
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('注销成功！');location.href='/Account/Login.aspx';</script>");
        }
        protected bool check_cookie()
        {

            HttpCookie UserNameCookie = Request.Cookies["UserNameCookie"];
            HttpCookie UserPasswordCookie = Request.Cookies["UserPasswordCookie"];
            if (UserNameCookie != null && UserPasswordCookie != null)
            {
                string str = "select * from Users where UserName='" + UserNameCookie.Values["UserName"].ToString() + "' and PassWord='" + UserPasswordCookie.Values["UserPassword"].ToString() + "'";

                DataSet ds = Helper.GetDataSet(str, "Users");
                if (ds.Tables[0].Rows.Count <= 0)
                {
                    return false;
                }
                else
                {
                    Session["UserID"] = ds.Tables[0].Rows[0]["UserID"].ToString();
                    Session["UserName"] = ds.Tables[0].Rows[0]["UserName"].ToString();
                    Session["User_Type"] = ds.Tables[0].Rows[0]["User_Type"].ToString();
                    username = Session["UserName"].ToString();
                    string type = Session["User_Type"].ToString();
                    if (type == "1")
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('登录成功，您的身份是customer');location.href='/CustomerPanel/MainPage.aspx';</script>");
                    else if (type == "2")
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('登录成功，您的身份是supplier');location.href='/SupplierPanel/MainPage.aspx';</script>");
                    else if (type == "3")
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('登录成功，您的身份是professor');location.href='/ProfessorPanel/MainPage.aspx';</script>");
                    else
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('登录成功，您的身份不能识别');location.href='/Default.aspx';</script>");
                    return true;
                }
            }
            return false;
        }
    }
}